package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description:
 * @author 岳子昂
 * @date 2022-01-20 09:49
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class ProductController {

    @Reference(timeout = 10000,retries = 2, check = false)
    IProductService iProductService;

    /**
     * @description:  显示商品详情信息
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-20 09:51
     */
    @RequestMapping("/product/**")
    @Anoymous
    public ResponseData product(HttpServletRequest httpServletRequest){

        // 首先获取商品id，被放在url的最后面
        String uri = httpServletRequest.getRequestURI();
        String[] splitURI = uri.split("/");
        String productId = splitURI[splitURI.length - 1];

        // 针对商品id进行校验
        Long id = null;
        try {
            id = Long.parseLong(productId);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseUtil().setErrorMsg("参数校验不合格");
        }

        // 创建request对象
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);

        // 利用request作为参数，调用service层的方法，获取response对象
        ProductDetailResponse response = iProductService.getProductDetail(request);

        // 如果结果的code与success的code相同，返回这个结果
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getProductDetailDto());
        }

        // 如果走到这里，说明发生了错误，返回错误信息
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }


}
